package com.lpf.back.controller;

import com.lpf.back.common.CommonRes;
import com.lpf.back.common.oss.Result;
import com.lpf.back.domain.Dynamic;
import com.lpf.back.domain.User;
import com.lpf.back.service.interfaces.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/oss")
public class OssController {
    @Autowired
    private OssService ossService;

    /**
     * 文件上传到阿里oss服务器
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public CommonRes upload(HttpServletRequest request, @RequestParam("avatarImage")MultipartFile file) {
//        System.out.println("hellowrld");
        User user = ossService.uploadPic(request, file);
        CommonRes res = new CommonRes();
        if (user == null) {
            res.setData(null);
            res.setMsg("上传失败！");
            res.setStatus(0);
            return res;
        }
        res.setMsg("上传成功！");
        res.setData(user);
        res.setStatus(1);
        return res;
    }

    @RequestMapping(value = "/dynamic", method = RequestMethod.POST)
    public CommonRes dynamic(HttpServletRequest request, @RequestParam("picture") MultipartFile picture){
        Dynamic dynamic = ossService.dynamic(request, picture);
        if(dynamic != null){
            return new CommonRes(dynamic, 1, "发布成功！");
        }
        return new CommonRes(null, 0, "发布失败！");
    }

    @RequestMapping(value = "/dynamicNoImg", method = RequestMethod.POST)
    public CommonRes dynamicNoImg(String releaseContent, Integer id){
        CommonRes res = new CommonRes();
        Dynamic dynamic = ossService.dynamicNoImg(releaseContent, id);
        if(dynamic != null){
            return new CommonRes(dynamic, 1, "发布成功！");
        }
        return new CommonRes(null, 0, "发布失败！");
    }
}
